// components/photo/photo.js

// 小程序组件的生命周期 
Component({
  /**
   * 组件的属性列表  props 
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    pic:'',
    src:'',
    items:['定位','录音','拨打电话','振动'],
    latitude :'',
    longitude:''
  },

  created(){
    console.log("created")
  },
  attached(){
    console.log("attached")
  },
  ready(){
    console.log("ready")
  },
  detached(){
    console.log("detached")
  },
  moved(){
    console.log("moved")
  },


  /**
   * 组件的方法列表
   */
  methods: {
    openWxmap(latitude,longitude){
      wx.openLocation({
        latitude,
        longitude,
        scale: 18,
        name:'千锋教育',
        address:"金融港B18栋3楼"
      })
    },  
    openaction(){
      wx.showActionSheet({
        itemList: this.data.items,
        success : (res)=> {
          console.log(res.tapIndex)
          if(res.tapIndex==0){
            wx.getLocation({
              type: 'wgs84',
              success :(res)=> {
                console.log(res)
                const latitude = res.latitude
                const longitude = res.longitude
                const speed = res.speed
                const accuracy = res.accuracy
                this.setData({
                  latitude,
                  longitude
                })
                this.openWxmap(latitude,longitude)
              }
             })
          }else if (res.tapIndex==1){
            wx.startRecord({
              success (res) {
                const tempFilePath = res.tempFilePath
              }
            })
            setTimeout(function () {
              wx.stopRecord() // 结束录音
            }, 10000 * 6 )
          }else if (res.tapIndex==2){
            wx.makePhoneCall({
              phoneNumber: '15878945612' //仅为示例，并非真实的电话号码
            })
          }else {
            wx.vibrateShort({
              type:"heavy"
            })
          }
        },
        fail (res) {
          console.log(res.errMsg)
        }
      })
    },  
    takePhoto(){
      const ctx = wx.createCameraContext()
      ctx.takePhoto({
        quality: 'high',
        success: (res) => {
          this.setData({
            src: res.tempImagePath
          })
        }
      })
    },
    startScan(){
      wx.scanCode({
        success (res) {
          console.log(res)
        }
      })
    },
    startPhoto(){
      wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success :(res)=> {
          // tempFilePath可以作为img标签的src属性显示图片
          const tempFilePaths = res.tempFilePaths
          this.setData({
            pic:tempFilePaths
          })
        }
      })
    }
  }
})
